import axios from 'axios'

const axiosInit = axios.create({
  baseURL: 'http://localhost:8081',
  timeout: 5000,
})

//解决后端sessionId不一样问题
axiosInit.defaults.withCredentials = true;

//请求拦截
axiosInit.interceptors.request.use(
  config => {
    if (sessionStorage.getItem("token")) {
      config.headers.token = sessionStorage.getItem("token")
    }
    return config
  },
  error => {
    console.log(error); //调试用
    return Promise.reject(error)
  }
);

//响应拦截
axiosInit.interceptors.response.use(
  response => {
    sessionStorage.setItem('token', response.headers.token);
    return response
  },
  error => {
    console.log(error); //调试用
    return Promise.reject(error)
  }
);

export default axiosInit;